from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from contact.models import ContactForm
from django.template import RequestContext, Context
from django.core.context_processors import csrf
from django.views.decorators.csrf import csrf_protect
from django.core.mail import send_mail

def contact(request):
    
    success = False
    name = ""
    email = ""
    subject = ""
    message = ""
    
    if request.method == 'POST': # If the form has been submitted...
        contact_form = ContactForm(request.POST) # A form bound to the POST data
        
        
        
        if contact_form.is_valid(): # All validation rules pass
            
            
            
            success = True
            
            name = contact_form.cleaned_data['name']
            email = contact_form.cleaned_data['email']
            subject = contact_form.cleaned_data['subject']
            message = contact_form.cleaned_data['message']
            
            send_mail("Subject", "Message: %s %s %s %s" % (name, email, subject, message), email, ['josh@joshd.co.uk'], fail_silently=False)
            
    else:
        contact_form = ContactForm()
            
    
    
    cxt = {'contact_form':contact_form, 'name':name, 'email':email, 'subject':subject, 'message':message, 'success':success, }
    
    return render_to_response('contact.html', cxt, context_instance=RequestContext(request))
            
           